package edu.hwadee.BEIS.aop;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.hwadee.BEIS.constants.CustomException;
import edu.hwadee.BEIS.constants.ResultEnum;
import edu.hwadee.BEIS.entity.JsonResult;
import edu.hwadee.BEIS.util.ResultUtil;

/**
 * 异常处理类
 * 捕获系统抛出的运行时异常
 * @author kingsong
 *
 */
@ControllerAdvice
public class ExceptionHandle {

	private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonResult handle(Exception e) {
        if (e instanceof CustomException) {
            CustomException customException = (CustomException) e;
            return ResultUtil.error(customException.getCode(), customException.getMessage());
        }else {
            logger.error("【系统异常】{}", e);
            return ResultUtil.error(ResultEnum.UNKNOWN_ERROR);
        }
    }
}
